home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_kernel_source / SCRIPTS / TKGEN.C < prev    next >
C/C++ Source or Header  |  1999-09-17  |  31KB  |  1,048 lines

  1. /* Generate tk script based upon config.in
  2.  *
  3.  * Version 1.0
  4.  * Eric Youngdale
  5.  * 10/95
  6.  *
  7.  * 1996 01 04
  8.  * Avery Pennarun - Aesthetic improvements.
  9.  *
  10.  * 1996 01 24
  11.  * Avery Pennarun - Bugfixes and more aesthetics.
  12.  *
  13.  * 1996 03 08
  14.  * Avery Pennarun - The int and hex config.in commands work right.
  15.  *                - Choice buttons are more user-friendly.
  16.  *                - Disabling a text entry line greys it out properly.
  17.  *                - dep_tristate now works like in Configure. (not pretty)
  18.  *                - No warnings in gcc -Wall. (Fixed some "interesting" bugs.)
  19.  *                - Faster/prettier "Help" lookups.
  20.  *
  21.  * 1996 03 15
  22.  * Avery Pennarun - Added new sed script from Axel Boldt to make help even
  23.  *                  faster. (Actually awk is downright slow on some machines.)
  24.  *                - Fixed a bug I introduced into Choice dependencies.  Thanks
  25.  *                  to Robert Krawitz for pointing this out.
  26.  *
  27.  * 1996 03 16
  28.  * Avery Pennarun - basic "do_make" support added to let sound config work.
  29.  *
  30.  * 1996 03 25
  31.  *     Axel Boldt - Help now works on "choice" buttons.
  32.  *
  33.  * 1996 04 06
  34.  * Avery Pennarun - Improved sound config stuff. (I think it actually works
  35.  *                  now!)
  36.  *                - Window-resize-limits don't use ugly /usr/lib/tk4.0 hack.
  37.  *                - int/hex work with tk3 again. (The "cget" error.)
  38.  *                - Next/Prev buttons switch between menus.  I can't take
  39.  *                  much credit for this; the code was already there, but
  40.  *                  ifdef'd out for some reason.  It flickers a lot, but
  41.  *                  I suspect there's no "easy" fix for that.
  42.  *                - Labels no longer highlight as you move the mouse over
  43.  *                  them (although you can still press them... oh well.)
  44.  *                - Got rid of the last of the literal color settings, to
  45.  *                  help out people with mono X-Windows systems. 
  46.  *                  (Apparently there still are some out there!)
  47.  *                - Tabstops seem sensible now.
  48.  *
  49.  * 1996 04 14
  50.  * Avery Pennarun - Reduced flicker when creating windows, even with "update
  51.  *                  idletasks" hack.
  52.  *
  53.  * 1997 12 08
  54.  * Michael Chastain - Remove sound driver special cases.
  55.  *
  56.  * 1997 11 15
  57.  * Michael Chastain - For choice buttons, write values for all options,
  58.  *                    not just the single chosen one.  This is compatible
  59.  *                    with 'make config' and 'make oldconfig', and is
  60.  *                    needed so smart-config dependencies work if the
  61.  *                    user switches from one configuration method to
  62.  *                    another.
  63.  *
  64.  * 1998 03 09
  65.  * Axel Boldt - Smaller layout of main menu - it's still too big for 800x600.
  66.  *            - Display help in text window to allow for cut and paste.
  67.  *            - Allow for empty lines in help texts.
  68.  *            - update_define should not set all variables unconditionally to
  69.  *              0: they may have been set to 1 elsewhere. CONFIG_NETLINK is
  70.  *              an example.
  71.  *
  72.  * 1999 01 04
  73.  * Michael Elizabeth Chastain <mec@shout.net>
  74.  * - Call clear_globalflags when writing out update_mainmenu.
  75.  *   This fixes the missing global/vfix lines for ARCH=alpha on 2.2.0-pre4.
  76.  *
  77.  * 8 January 1999, Michael Elizabeth Chastain <mec@shout.net>
  78.  * - Emit menus_per_column
  79.  *
  80.  * 14 January 1999, Michael Elizabeth Chastain <mec@shout.net>
  81.  * - Steam-clean this file.  I tested this by generating kconfig.tk for every
  82.  *   architecture and comparing it character-for-character against the output
  83.  *   of the old tkparse.
  84.  * - Fix flattening of nested menus.  The old code simply assigned items to
  85.  *   the most recent token_mainmenu_option, without paying attention to scope.
  86.  *   For example: "menu-1 bool-a menu-2 bool-b endmenu bool-c bool-d endmenu".
  87.  *   The old code would put bool-a in menu-1, bool-b in menu-2, and bool-c
  88.  *   and bool-d in *menu-2*.  This hosed the nested submenus in
  89.  *   drives/net/Config.in and other places.
  90.  * - Fix menu line wraparound at 128 menus (some fool used a 'char' for
  91.  *   a counter).
  92.  *
  93.  * 23 January 1999, Michael Elizabeth Chastain <mec@shout.net>
  94.  * - Remove bug-compatible code.
  95.  */
  96.  
  97. #include <stdio.h>
  98. #include <unistd.h>
  99. #include "tkparse.h"
  100.  
  101.  
  102.  
  103. /*
  104.  * Total number of menus.
  105.  */
  106. static int tot_menu_num = 0;
  107.  
  108.  
  109.  
  110. /*
  111.  * Generate portion of wish script for the beginning of a submenu.
  112.  * The guts get filled in with the various options.
  113.  */
  114. static void start_proc( char * label, int menu_num, int flag )
  115. {
  116.     if ( flag )
  117.     printf( "menu_option menu%d %d \"%s\"\n", menu_num, menu_num, label );
  118.     printf( "proc menu%d {w title} {\n", menu_num );
  119.     printf( "\tcatch {destroy $w}\n" );
  120.     printf( "\ttoplevel $w -class Dialog\n" );
  121.     printf( "\twm withdraw $w\n" );
  122.     printf( "\tmessage $w.m -width 400 -aspect 300 -text \\\n" );
  123.     printf( "\t\t\"%s\"  -relief raised\n", label );
  124.     printf( "\tpack $w.m -pady 10 -side top -padx 10\n" );
  125.     printf( "\twm title $w \"%s\" \n\n", label );
  126.  
  127.     /*
  128.      * Attach the "Prev", "Next" and "OK" buttons at the end of the window.
  129.      */
  130.     printf( "\tset oldFocus [focus]\n" );
  131.     printf( "\tframe $w.f\n" );
  132.     printf( "\tbutton $w.f.back -text \"Main Menu\" \\\n" );
  133.     printf( "\t\t-width 15 -command \"destroy $w; focus $oldFocus; update_mainmenu $w\"\n" );
  134.     printf( "\tbutton $w.f.next -text \"Next\" \\\n" );
  135.     printf( "\t\t-width 15 -command \" destroy $w; focus $oldFocus;  menu%d .menu%d \\\"$title\\\"\"\n", menu_num+1, menu_num+1 );
  136.     if ( menu_num == tot_menu_num )
  137.     printf( "\t$w.f.next configure -state disabled\n" );
  138.     printf( "\tbutton $w.f.prev -text \"Prev\" \\\n" );
  139.     printf( "\t\t-width 15 -command \" destroy $w; focus $oldFocus; menu%d .menu%d \\\"$title\\\"\"\n", menu_num-1, menu_num-1 );
  140.     if ( menu_num == 1 )
  141.     printf( "\t$w.f.prev configure -state disabled\n" );
  142.     printf( "\tpack $w.f.back $w.f.next $w.f.prev -side left -expand on\n" );
  143.     printf( "\tpack $w.f -pady 10 -side bottom -anchor w -fill x\n" );
  144.  
  145.     /*
  146.      * Lines between canvas and other areas of the window.
  147.      */
  148.     printf( "\tframe $w.topline -relief ridge -borderwidth 2 -height 2\n" );
  149.     printf( "\tpack $w.topline -side top -fill x\n\n" );
  150.     printf( "\tframe $w.botline -relief ridge -borderwidth 2 -height 2\n" );
  151.     printf( "\tpack $w.botline -side bottom -fill x\n\n" );
  152.  
  153.     /*
  154.      * The "config" frame contains the canvas and a scrollbar.
  155.      */
  156.     printf( "\tframe $w.config\n" );
  157.     printf( "\tpack $w.config -fill y -expand on\n\n" );
  158.     printf( "\tscrollbar $w.config.vscroll -command \"$w.config.canvas yview\"\n" );
  159.     printf( "\tpack $w.config.vscroll -side right -fill y\n\n" );
  160.  
  161.     /*
  162.      * The scrollable canvas itself, where the real work (and mess) gets done.
  163.      */
  164.     printf( "\tcanvas $w.config.canvas -height 1\\\n" );
  165.     printf( "\t\t-relief flat -borderwidth 0 -yscrollcommand \"$w.config.vscroll set\" \\\n" );
  166.     printf( "\t\t-width [expr [winfo screenwidth .] * 1 / 2] \n" );
  167.     printf( "\tframe $w.config.f\n" );
  168.     printf( "\tpack $w.config.canvas -side right -fill y\n" );
  169.     printf("\n\n");
  170. }
  171.  
  172.  
  173.  
  174. /*
  175.  * Each proc we create needs a global declaration for any global variables we
  176.  * use.  To minimize the size of the file, we set a flag each time we output
  177.  * a global declaration so we know whether we need to insert one for a
  178.  * given function or not.
  179.  */
  180. void clear_globalflags( struct kconfig * scfg )
  181. {
  182.     struct kconfig * cfg;
  183.  
  184.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  185.     cfg->global_written = 0;
  186. }
  187.  
  188.  
  189.  
  190. /*
  191.  * Output a "global" line for a given variable.  Also include the
  192.  * call to "vfix".  (If vfix is not needed, then it's fine to just printf
  193.  * a "global" line).
  194.  */
  195. void global( const char *var )
  196. {
  197.     printf( "\tglobal %s; vfix %s\n", var, var );
  198. }
  199.  
  200.  
  201.  
  202. /*
  203.  * This function walks the chain of conditions that we got from cond.c
  204.  * and creates a TCL conditional to enable/disable a given widget.
  205.  */
  206. void generate_if( struct kconfig * cfg, struct condition * ocond,
  207.     int menu_num, int line_num )
  208. {
  209.     struct condition * cond;
  210.  
  211.     /*
  212.      * First write any global declarations we need for this conditional.
  213.      */
  214.     for ( cond = ocond; cond != NULL; cond = cond->next )
  215.     {
  216.     switch ( cond->op )
  217.     {
  218.     default:
  219.         break;
  220.  
  221.     case op_variable:
  222.         global( cond->str );
  223.         break;
  224.  
  225.     case op_kvariable:
  226.         if ( ! cond->cfg->global_written )
  227.         {
  228.         cond->cfg->global_written = 1;
  229.         global( cond->cfg->optionname );
  230.         }
  231.         break;
  232.     }
  233.     }
  234.  
  235.     /*
  236.      * Now write this option.
  237.      */
  238.     if ( ! cfg->global_written && cfg->optionname != NULL )
  239.     {
  240.     cfg->global_written = 1;
  241.     global( cfg->optionname );
  242.     }
  243.  
  244.     /*
  245.      * Generate the body of the conditional.
  246.      */
  247.     printf( "\tif {" );
  248.     for ( cond = ocond; cond != NULL; cond = cond->next )
  249.     {
  250.     switch ( cond->op )
  251.     {
  252.     default:
  253.         break;
  254.  
  255.     case op_bang:   printf( " ! "  ); break;
  256.     case op_eq:     printf( " == " ); break;
  257.     case op_neq:    printf( " != " ); break;
  258.     case op_and:    printf( " && " ); break;
  259.     case op_and1:   printf( " && " ); break;
  260.     case op_or:     printf( " || " ); break;
  261.     case op_lparen: printf( "("    ); break;
  262.     case op_rparen: printf( ")"    ); break;
  263.  
  264.     case op_variable:
  265.         printf( "$%s", cond->str );
  266.         break;
  267.  
  268.     case op_kvariable:
  269.         printf( "$%s", cond->cfg->optionname );
  270.         break;
  271.  
  272.     case op_constant:
  273.         if      ( strcmp( cond->str, "y" ) == 0 ) printf( "1" );
  274.         else if ( strcmp( cond->str, "n" ) == 0 ) printf( "0" );
  275.         else if ( strcmp( cond->str, "m" ) == 0 ) printf( "2" );
  276.         else
  277.         printf( "\"%s\"", cond->str );
  278.         break;
  279.     }
  280.     }
  281.     printf( "} then { " );
  282.  
  283.     /*
  284.      * Generate a procedure call to write the value.
  285.      * This code depends on procedures in header.tk.
  286.      */
  287.     switch ( cfg->token )
  288.     {
  289.     default:
  290.     printf( " }\n" );
  291.     break;
  292.  
  293.     case token_bool:
  294.     printf( ".menu%d.config.f.x%d.y configure -state normal;",
  295.         menu_num, line_num );
  296.     printf( ".menu%d.config.f.x%d.n configure -state normal;",
  297.         menu_num, line_num );
  298.     printf( ".menu%d.config.f.x%d.l configure -state normal;",
  299.         menu_num, line_num );
  300.     printf( "set %s [expr $%s&15];",
  301.         cfg->optionname, cfg->optionname );
  302.     printf( "} else { ");
  303.     printf( ".menu%d.config.f.x%d.y configure -state disabled;",
  304.         menu_num, line_num );
  305.     printf( ".menu%d.config.f.x%d.n configure -state disabled;",
  306.         menu_num, line_num );
  307.     printf( ".menu%d.config.f.x%d.l configure -state disabled;",
  308.         menu_num, line_num );
  309.     printf( "set %s [expr $%s|16];}\n",
  310.         cfg->optionname, cfg->optionname );
  311.     break;
  312.  
  313.     case token_choice_header:
  314.     fprintf( stderr, "Internal error on token_choice_header\n" );
  315.     exit( 1 );
  316.  
  317.     case token_choice_item:
  318.     fprintf( stderr, "Internal error on token_choice_item\n" );
  319.     exit( 1 );
  320.  
  321.     case token_define_bool:
  322.     printf( "set %s %s } \n",
  323.         cfg->optionname, cfg->value );
  324.     break;
  325.  
  326.     case token_dep_tristate:
  327.     case token_tristate:
  328.     if ( cfg->token == token_dep_tristate )
  329.     {
  330.         global( cfg->depend );
  331.         printf( "if { $%s != 1 && $%s != 0 } then {",
  332.         cfg->depend, cfg->depend );
  333.         printf( ".menu%d.config.f.x%d.y configure -state disabled;",
  334.         menu_num, line_num );
  335.         printf( "} else {" );
  336.         printf( ".menu%d.config.f.x%d.y configure -state normal;",
  337.         menu_num, line_num);
  338.         printf( "}; " );
  339.     }
  340.     else
  341.     {
  342.         printf( ".menu%d.config.f.x%d.y configure -state normal;",
  343.         menu_num, line_num );
  344.     }
  345.  
  346.     printf( ".menu%d.config.f.x%d.n configure -state normal;",
  347.         menu_num, line_num );
  348.     printf( "global CONFIG_MODULES; if {($CONFIG_MODULES == 1)} then { .menu%d.config.f.x%d.m configure -state normal };",
  349.         menu_num, line_num );
  350.     printf( ".menu%d.config.f.x%d.l configure -state normal;",
  351.         menu_num, line_num );
  352.  
  353.     /*
  354.      * Or in a bit to the variable - this causes all of the radiobuttons
  355.      * to be deselected (i.e. not be red).
  356.      */
  357.     printf( "set %s [expr $%s&15];",
  358.         cfg->optionname, cfg->optionname );
  359.     printf( "} else { " );
  360.     printf( ".menu%d.config.f.x%d.y configure -state disabled;",
  361.         menu_num, line_num );
  362.     printf( ".menu%d.config.f.x%d.n configure -state disabled;",
  363.         menu_num, line_num );
  364.     printf( ".menu%d.config.f.x%d.m configure -state disabled;",
  365.         menu_num, line_num );
  366.     printf( ".menu%d.config.f.x%d.l configure -state disabled;",
  367.         menu_num, line_num );
  368.  
  369.     /*
  370.      * Clear the disable bit to enable the correct radiobutton.
  371.      */
  372.     printf( "set %s [expr $%s|16];}\n",
  373.         cfg->optionname, cfg->optionname );
  374.     break;
  375.  
  376.     case token_hex:
  377.     case token_int:
  378.     case token_string:
  379.     printf( ".menu%d.config.f.x%d.x configure -state normal -foreground [ cget .ref -foreground ]; ",
  380.         menu_num, line_num);
  381.     printf( ".menu%d.config.f.x%d.l configure -state normal; ",
  382.         menu_num, line_num);
  383.     printf( "} else { " );
  384.     printf( ".menu%d.config.f.x%d.x configure -state disabled -foreground [ cget .ref -disabledforeground ];",
  385.         menu_num, line_num );
  386.     printf( ".menu%d.config.f.x%d.l configure -state disabled;}\n",
  387.         menu_num, line_num );
  388.     break;
  389.  
  390.     case token_mainmenu_option:
  391.     printf( ".f0.x%d configure -state normal } else { .f0.x%d configure -state disabled }\n",
  392.         menu_num, menu_num );
  393.     break;
  394.     }
  395. }
  396.  
  397.  
  398.  
  399. /*
  400.  * Generate a line that writes a variable to the output file.
  401.  */
  402. void generate_writeconfig( struct kconfig * cfg )
  403. {
  404.     struct condition * cond;
  405.  
  406.     /*
  407.      * Generate global declaration for this symbol.
  408.      */
  409.     if ( cfg->token != token_comment )
  410.     {
  411.     if ( ! cfg->global_written )
  412.     {
  413.         cfg->global_written = 1;
  414.         printf( "\tglobal %s\n", cfg->optionname );
  415.     }
  416.     }
  417.  
  418.     /*
  419.      * Generate global declarations for the condition chain.
  420.      */
  421.     for ( cond = cfg->cond; cond != NULL; cond = cond->next )
  422.     {
  423.     switch( cond->op )
  424.     {
  425.     default:
  426.         break;
  427.  
  428.     case op_variable:
  429.         global( cond->str );
  430.         break;
  431.  
  432.     case op_kvariable:
  433.         if ( ! cond->cfg->global_written )
  434.         {
  435.         cond->cfg->global_written = 1;
  436.         global( cond->cfg->optionname );
  437.         }
  438.         break;
  439.     }
  440.     }
  441.  
  442.     /*
  443.      * Generate indentation.
  444.      */
  445.     if ( cfg->token != token_choice_header )
  446.     printf( "\t" );
  447.  
  448.     /*
  449.      * Generate the conditional.
  450.      */
  451.     if ( cfg->cond != NULL )
  452.     {
  453.     printf( "if {" );
  454.     for ( cond = cfg->cond; cond != NULL; cond = cond->next )
  455.     {
  456.         switch ( cond->op )
  457.         {
  458.         default:           break;
  459.         case op_bang:      printf( " ! "  ); break;
  460.         case op_eq:        printf( " == " ); break;
  461.         case op_neq:       printf( " != " ); break;
  462.         case op_and:       printf( " && " ); break;
  463.         case op_and1:      printf( " && " ); break;
  464.         case op_or:        printf( " || " ); break;
  465.         case op_lparen:    printf( "("    ); break;
  466.         case op_rparen:    printf( ")"    ); break;
  467.  
  468.         case op_variable:
  469.         printf( "$%s", cond->str );
  470.         break;
  471.  
  472.         case op_kvariable:
  473.         printf( "$%s", cond->cfg->optionname );
  474.         break;
  475.  
  476.         case op_constant:
  477.         if      ( strcmp( cond->str, "n" ) == 0 ) printf( "0" );
  478.         else if ( strcmp( cond->str, "y" ) == 0 ) printf( "1" );
  479.         else if ( strcmp( cond->str, "m" ) == 0 ) printf( "2" );
  480.         else
  481.             printf( "\"%s\"", cond->str );
  482.         break;
  483.         }
  484.     }
  485.     printf( "} then {" );
  486.     }
  487.  
  488.     /*
  489.      * Generate a procedure call to write the value.
  490.      * This code depends on the write_* procedures in header.tk.
  491.      */
  492.     switch ( cfg->token )
  493.     {
  494.     default:
  495.     if ( cfg->cond != NULL )
  496.         printf( " }" );
  497.     printf( "\n" );
  498.     break;
  499.  
  500.     case token_bool:
  501.     case token_tristate:
  502.     if ( cfg->cond )
  503.         printf( " " );
  504.     printf( "write_tristate $cfg $autocfg %s $%s $notmod", 
  505.         cfg->optionname, cfg->optionname );
  506.     if ( cfg->cond != NULL )
  507.         printf( " }" );
  508.     printf( "\n" );
  509.     break;
  510.  
  511.     case token_choice_header:
  512.     /*
  513.      * This is funky code -- it fails if there were any conditionals.
  514.      * Fortunately all the conditionals got stripped off somewhere
  515.      * else.
  516.      */
  517.     {
  518.         struct kconfig * cfg1;
  519.         for ( cfg1  = cfg->next;
  520.           cfg1 != NULL && cfg1->token == token_choice_item;
  521.           cfg1  = cfg1->next )
  522.         {
  523.         printf("\tif { $%s == \"%s\" } then { write_tristate $cfg $autocfg %s 1 $notmod } else { write_tristate $cfg $autocfg %s 0 $notmod }\n",
  524.             cfg->optionname, cfg1->label,
  525.             cfg1->optionname,
  526.             cfg1->optionname );
  527.         }
  528.     }
  529.     break;
  530.  
  531.     case token_choice_item:
  532.     fprintf( stderr, "Internal error on token_choice_item\n" );
  533.     exit( 1 );
  534.  
  535.     case token_comment:
  536.     printf( "write_comment $cfg $autocfg \"%s\"",
  537.         cfg->label );
  538.     if ( cfg->cond != NULL )
  539.         printf( "}" );
  540.     printf( "\n" );
  541.     break;
  542.  
  543.     case token_define_bool:
  544.     if ( cfg->cond == NULL )
  545.     {
  546.         printf( "write_tristate $cfg $autocfg %s $%s $notmod\n",
  547.         cfg->optionname, cfg->optionname );
  548.     }
  549.     else
  550.     {
  551.         printf( "write_tristate $cfg $autocfg %s %s $notmod }\n",
  552.         cfg->optionname, cfg->value );
  553.     }
  554.     break;
  555.  
  556.     case token_dep_tristate:
  557.     if ( cfg->cond )
  558.         printf( " " );
  559.     printf( "write_tristate $cfg $autocfg %s $%s $%s",
  560.         cfg->optionname, cfg->optionname, cfg->depend );
  561.     if ( cfg->cond != NULL )
  562.         printf( " }" );
  563.     printf( " \n" );
  564.     break;
  565.  
  566.     case token_hex:
  567.     if ( cfg->cond != NULL )
  568.         printf( " " );
  569.     printf( "write_hex $cfg $autocfg %s $%s $notmod",
  570.         cfg->optionname, cfg->optionname );
  571.     if ( cfg->cond != NULL )
  572.         printf( " }" );
  573.     printf( "\n" );
  574.     break;
  575.  
  576.     case token_int:
  577.     if ( cfg->cond != NULL )
  578.         printf( " " );
  579.     printf( "write_int $cfg $autocfg %s $%s $notmod",
  580.         cfg->optionname, cfg->optionname );
  581.     if ( cfg->cond != NULL )
  582.         printf( " }" );
  583.     printf( "\n" );
  584.     break;
  585.  
  586.     case token_string:
  587.     if ( cfg->cond != NULL )
  588.         printf( " " );
  589.     printf( "write_string $cfg $autocfg %s $%s $notmod",
  590.         cfg->optionname, cfg->optionname );
  591.     if ( cfg->cond != NULL )
  592.         printf( " }" );
  593.     printf( "\n" );
  594.     break;
  595.     }
  596. }
  597.  
  598.  
  599.  
  600. /*
  601.  * Generates the end of a menu procedure.
  602.  */
  603. static void end_proc( struct kconfig * scfg, int menu_num )
  604. {
  605.     struct kconfig * cfg;
  606.  
  607.     printf( "\n\n\n" );
  608.     printf( "\tfocus $w\n" );
  609.     printf( "\tupdate_menu%d $w.config.f\n",
  610.     menu_num );
  611.     printf( "\tglobal winx; global winy\n" );
  612.     printf( "\tset winx [expr [winfo x .]+30]; set winy [expr [winfo y .]+30]\n" );
  613.     printf( "\twm geometry $w +$winx+$winy\n" );
  614.  
  615.     /*
  616.      * Now that the whole window is in place, we need to wait for an "update"
  617.      * so we can tell the canvas what its virtual size should be.
  618.      *
  619.      * Unfortunately, this causes some ugly screen-flashing because the whole
  620.      * window is drawn, and then it is immediately resized.  It seems
  621.      * unavoidable, though, since "frame" objects won't tell us their size
  622.      * until after an update, and "canvas" objects can't automatically pack
  623.      * around frames.  Sigh.
  624.      */
  625.     printf( "\tupdate idletasks\n" );
  626.     printf( "\t$w.config.canvas create window 0 0 -anchor nw -window $w.config.f\n\n" );
  627.     printf( "\t$w.config.canvas configure \\\n" );
  628.     printf( "\t\t-width [expr [winfo reqwidth $w.config.f] + 1]\\\n" );
  629.     printf( "\t\t-scrollregion \"-1 -1 [expr [winfo reqwidth $w.config.f] + 1] \\\n" );
  630.     printf( "\t\t\t [expr [winfo reqheight $w.config.f] + 1]\"\n\n" );
  631.      
  632.     /*
  633.      * If the whole canvas will fit in 3/4 of the screen height, do it;
  634.      * otherwise, resize to around 1/2 the screen and let us scroll.
  635.      */
  636.     printf( "\tset winy [expr [winfo reqh $w] - [winfo reqh $w.config.canvas]]\n" );
  637.     printf( "\tset scry [expr [winfo screenh $w] / 2]\n" );
  638.     printf( "\tset maxy [expr [winfo screenh $w] * 3 / 4]\n" );
  639.     printf( "\tset canvtotal [expr [winfo reqh $w.config.f] + 2]\n" );
  640.     printf( "\tif [expr $winy + $canvtotal < $maxy] {\n" );
  641.     printf( "\t\t$w.config.canvas configure -height $canvtotal\n" );
  642.     printf( "\t} else {\n" );
  643.     printf( "\t\t$w.config.canvas configure -height [expr $scry - $winy]\n" );
  644.     printf( "\t}\n" );
  645.  
  646.     /*
  647.      * Limit the min/max window size.  Height can vary, but not width,
  648.      * because of the limitations of canvas and our laziness.
  649.      */
  650.     printf( "\tupdate idletasks\n" );
  651.     printf( "\twm maxsize $w [winfo width $w] [winfo screenheight $w]\n" );
  652.     printf( "\twm minsize $w [winfo width $w] 100\n\n" );
  653.     printf( "\twm deiconify $w\n" );
  654.     printf( "}\n\n\n" );
  655.  
  656.     /*
  657.      * Now we generate the companion procedure for the menu we just
  658.      * generated.  This procedure contains all of the code to
  659.      * disable/enable widgets based upon the settings of the other
  660.      * widgets, and will be called first when the window is mapped,
  661.      * and each time one of the buttons in the window are clicked.
  662.      */
  663.     printf( "proc update_menu%d {w}  {\n", menu_num );
  664.     printf( "\tupdate_define\n" );
  665.  
  666.     /*
  667.      * Clear all of the booleans that are defined in this menu.
  668.      */
  669.     clear_globalflags( scfg );
  670.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  671.     {
  672.     if ( cfg->menu_number == menu_num && cfg->token == token_define_bool
  673.     &&   cfg->optionname  != NULL )
  674.     {
  675.         if ( ! cfg->global_written )
  676.         {
  677.         cfg->global_written = 1;
  678.         printf( "\tglobal %s\n", cfg->optionname );
  679.         printf( "\tset %s 0\n",  cfg->optionname );
  680.         }
  681.     }
  682.     }
  683.  
  684.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  685.     {
  686.     if ( cfg->menu_number == menu_num
  687.     &&   cfg->token != token_mainmenu_option
  688.     &&   cfg->token != token_choice_item )
  689.     {
  690.         if ( cfg->cond != NULL )
  691.         generate_if( cfg, cfg->cond, cfg->menu_number, cfg->menu_line );
  692.         else
  693.         {
  694.         /*
  695.          * Treat tristate like conditional here.
  696.          */
  697.         if ( cfg->token == token_dep_tristate )
  698.         {
  699.             global( cfg->depend );
  700.             printf( "\tif {$%s != 1 && $%s != 0 } then { .menu%d.config.f.x%d.y configure -state disabled } else { .menu%d.config.f.x%d.y configure -state normal}\n",
  701.             cfg->depend, cfg->depend,
  702.             menu_num, cfg->menu_line,
  703.             menu_num, cfg->menu_line );
  704.         }
  705.         }
  706.     }
  707.     }
  708.  
  709.     printf("}\n\n\n");
  710. }
  711.  
  712.  
  713.  
  714. /*
  715.  * This is the top level function for generating the tk script.
  716.  */
  717. void dump_tk_script( struct kconfig * scfg )
  718. {
  719.     int menu_depth;
  720.     int menu_num [64];
  721.     struct kconfig * menu_first [256];
  722.     struct kconfig * menu_last  [256];
  723.     int imenu;
  724.     struct kconfig * cfg;
  725.     struct kconfig * cfg1 = NULL;
  726.     const char * name = "No Name";
  727.  
  728.     /*
  729.     * Thread the menu pointers so I can walk each menu separately.
  730.     */
  731.     tot_menu_num = 0;
  732.     menu_depth   = 0;
  733.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  734.     {
  735.     switch ( cfg->token )
  736.     {
  737.     default:
  738.         break;
  739.  
  740.     case token_mainmenu_name:
  741.         name = cfg->label;
  742.         break;
  743.  
  744.     case token_mainmenu_option:
  745.         if ( ++menu_depth >= 64 )
  746.         { fprintf( stderr, "menus too deep\n" ); exit( 1 ); }
  747.         if ( ++tot_menu_num >= 256 )
  748.         { fprintf( stderr, "too many menus\n" ); exit( 1 ); }
  749.         menu_num   [menu_depth]   = tot_menu_num;
  750.         menu_first [tot_menu_num] = cfg;
  751.         menu_last  [tot_menu_num] = cfg;
  752.         break;
  753.  
  754.     case token_endmenu:
  755.         /* flatten menus with proper scoping */
  756.         if ( --menu_depth < 0 )
  757.         { fprintf( stderr, "unmatched endmenu\n" ); exit( 1 ); }
  758.         break;
  759.  
  760.     case token_bool:
  761.     case token_choice_header:
  762.     case token_choice_item:
  763.     case token_dep_tristate:
  764.     case token_hex:
  765.     case token_int:
  766.     case token_string:
  767.     case token_tristate:
  768.         if ( menu_depth == 0 )
  769.         { fprintf( stderr, "statement not in menu\n" ); exit( 1 ); }
  770.         menu_last [menu_num [menu_depth]]->menu_next = cfg;
  771.         menu_last [menu_num [menu_depth]]            = cfg;
  772.         cfg->menu_next                               = NULL;
  773.         break;
  774.  
  775.     case token_define_bool:
  776.         break;
  777.     }
  778.     }
  779.  
  780.     /*
  781.      * Generate menus per column setting.
  782.      * There are:
  783.      *   four extra buttons for save/quit/load/store;
  784.      *   one blank button
  785.      *   add two to round up for division
  786.      */
  787.     printf( "set menus_per_column %d\n\n", (tot_menu_num + 4 + 1 + 2) / 3 );
  788.  
  789.     /*
  790.      * Generate the menus.
  791.      */
  792.     printf( "mainmenu_name \"%s\"\n", name );
  793.     for ( imenu = 1; imenu <= tot_menu_num; ++imenu )
  794.     {
  795.     int menu_line = 0;
  796.  
  797.     clear_globalflags( scfg );
  798.     start_proc( menu_first[imenu]->label, imenu, 1 );
  799.  
  800.     for ( cfg = menu_first[imenu]; cfg != NULL; cfg = cfg->menu_next )
  801.     {
  802.         cfg->menu_number = imenu;
  803.  
  804.         switch ( cfg->token )
  805.         {
  806.         default:
  807.         break;
  808.  
  809.         case token_bool:
  810.         cfg->menu_line = menu_line++;
  811.         printf( "\tbool $w.config.f %d %d \"%s\" %s\n",
  812.             cfg->menu_number, cfg->menu_line, cfg->label,
  813.             cfg->optionname );
  814.         break;
  815.  
  816.         case token_choice_header:
  817.         /*
  818.          * I need the first token_choice_item to pick out the right
  819.          * help text from Documentation/Configure.help.
  820.          */
  821.         cfg->menu_line = menu_line++;
  822.         printf( "\tglobal %s\n", cfg->optionname );
  823.         printf( "\tminimenu $w.config.f %d %d \"%s\" %s %s\n",
  824.             cfg->menu_number, cfg->menu_line, cfg->label,
  825.             cfg->optionname, cfg->next->optionname );
  826.         printf( "\tmenu $w.config.f.x%d.x.menu\n", cfg->menu_line );
  827.         cfg1 = cfg;
  828.         break;
  829.  
  830.         case token_choice_item:
  831.         /* note: no menu line; uses choice header menu line */
  832.         printf( "\t$w.config.f.x%d.x.menu add radiobutton -label \"%s\" -variable %s -value \"%s\" -command \"update_menu%d .menu%d.config.f\"\n",
  833.             cfg1->menu_line, cfg->label, cfg1->optionname,
  834.             cfg->label, cfg1->menu_number, cfg1->menu_number );
  835.         break;
  836.  
  837.         case token_dep_tristate:
  838.         cfg->menu_line = menu_line++;
  839.         printf( "\tdep_tristate $w.config.f %d %d \"%s\" %s %s\n",
  840.             cfg->menu_number, cfg->menu_line, cfg->label,
  841.             cfg->optionname, cfg->depend );
  842.         break;
  843.  
  844.         case token_hex:
  845.         cfg->menu_line = menu_line++;
  846.         printf( "\thex $w.config.f %d %d \"%s\" %s\n",
  847.             cfg->menu_number, cfg->menu_line, cfg->label,
  848.             cfg->optionname );
  849.         break;
  850.  
  851.         case token_int:
  852.         cfg->menu_line = menu_line++;
  853.         printf( "\tint $w.config.f %d %d \"%s\" %s\n",
  854.             cfg->menu_number, cfg->menu_line, cfg->label,
  855.             cfg->optionname );
  856.         break;
  857.  
  858.         case token_string:
  859.         cfg->menu_line = menu_line++;
  860.         printf( "\tistring $w.config.f %d %d \"%s\" %s\n",
  861.             cfg->menu_number, cfg->menu_line, cfg->label,
  862.             cfg->optionname );
  863.         break;
  864.  
  865.         case token_tristate:
  866.         cfg->menu_line = menu_line++;
  867.         printf( "\ttristate $w.config.f %d %d \"%s\" %s\n",
  868.             cfg->menu_number, cfg->menu_line, cfg->label,
  869.             cfg->optionname );
  870.         break;
  871.         }
  872.     }
  873.  
  874.     end_proc( scfg, imenu );
  875.     }
  876.  
  877.     /*
  878.      * The top level menu also needs an update function.  When we exit a
  879.      * submenu, we may need to disable one or more of the submenus on
  880.      * the top level menu, and this procedure will ensure that things are
  881.      * correct.
  882.      */
  883.     clear_globalflags( scfg );
  884.     printf( "proc update_mainmenu {w}  {\n" );
  885.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  886.     {
  887.     if ( cfg->token == token_mainmenu_option && cfg->cond != NULL )
  888.         generate_if( cfg, cfg->cond, cfg->menu_number, cfg->menu_line );
  889.     }
  890.     printf( "}\n\n\n" );
  891.  
  892. #if 0
  893.     /*
  894.      * Generate code to set the variables that are "defined".
  895.      */
  896.     for ( cfg = config; cfg != NULL; cfg = cfg->next )
  897.     {
  898.     if ( cfg->token == token_define_bool )
  899.     {
  900.         if ( cfg->cond != NULL ) 
  901.         generate_if( cfg, cfg->cond, menu_num, cfg->menu_line );
  902.         else
  903.         printf( "\twrite_define %s %s\n", cfg->optionname, cfg->value );
  904.     }
  905.     }
  906.     #endif
  907.  
  908.     /*
  909.      * Generate code to load the default settings into the variables.
  910.      * The script in tail.tk will attempt to load .config,
  911.      * which may override these settings, but that's OK.
  912.      */
  913.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  914.     {
  915.     switch ( cfg->token )
  916.     {
  917.     default:
  918.         break;
  919.  
  920.     case token_bool:
  921.     case token_choice_item:
  922.     case token_dep_tristate:
  923.     case token_tristate:
  924.         printf( "set %s 0\n", cfg->optionname );
  925.         break;
  926.  
  927.     case token_choice_header:
  928.         printf( "set %s \"(not set)\"\n", cfg->optionname );
  929.         break;
  930.  
  931.     case token_hex:
  932.     case token_int:
  933.     case token_string:
  934.         printf( "set %s %s\n", cfg->optionname, cfg->value );
  935.         break;
  936.     }
  937.     }
  938.  
  939.     /*
  940.      * Generate a function to write all of the variables to a file.
  941.      */
  942.     printf( "proc writeconfig {file1 file2} {\n" );
  943.     printf( "\tset cfg [open $file1 w]\n" );
  944.     printf( "\tset autocfg [open $file2 w]\n" );
  945.     printf( "\tset notmod 1\n" );
  946.     printf( "\tset notset 0\n" );
  947.     printf( "\tputs $cfg \"#\"\n");
  948.     printf( "\tputs $cfg \"# Automatically generated make config: don't edit\"\n");
  949.     printf( "\tputs $cfg \"#\"\n" );
  950.  
  951.     printf( "\tputs $autocfg \"/*\"\n" );
  952.     printf( "\tputs $autocfg \" * Automatically generated C config: don't edit\"\n" );
  953.     printf( "\tputs $autocfg \" */\"\n" );
  954.     printf( "\tputs $autocfg \"#define AUTOCONF_INCLUDED\"\n" );
  955.  
  956.     clear_globalflags( scfg );
  957.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  958.     {
  959.     switch ( cfg->token )
  960.     {
  961.     default:
  962.         break;
  963.  
  964.     case token_bool:
  965.     case token_choice_header:
  966.     case token_comment:
  967.     case token_define_bool:
  968.     case token_dep_tristate:
  969.     case token_hex:
  970.     case token_int:
  971.     case token_string:
  972.     case token_tristate:
  973.         generate_writeconfig( cfg );
  974.         break;
  975.     }
  976.     }
  977.     printf( "\tclose $cfg\n" );
  978.     printf( "\tclose $autocfg\n" );
  979.     printf( "}\n\n\n" );
  980.  
  981.     /*
  982.      * Generate a simple function that updates the master choice
  983.      * variable depending upon what values were loaded from a .config
  984.      * file.  
  985.      */
  986.     printf( "proc clear_choices { } {\n" );
  987.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  988.     {
  989.     if ( cfg->token == token_choice_header )
  990.     {
  991.         for ( cfg1  = cfg->next; 
  992.           cfg1 != NULL && cfg1->token == token_choice_item;
  993.           cfg1  = cfg1->next )
  994.         {
  995.         printf( "\tglobal %s; set %s 0\n",
  996.             cfg1->optionname, cfg1->optionname );
  997.         }
  998.     }
  999.     }
  1000.     printf( "}\n\n\n" );
  1001.  
  1002.     printf( "proc update_choices { } {\n" );
  1003.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  1004.     {
  1005.     if ( cfg->token == token_choice_header )
  1006.     {
  1007.         printf( "\tglobal %s\n", cfg->optionname );
  1008.         for ( cfg1  = cfg->next; 
  1009.           cfg1 != NULL && cfg1->token == token_choice_item;
  1010.           cfg1  = cfg1->next )
  1011.         {
  1012.         printf( "\tglobal %s\n", cfg1->optionname );
  1013.         printf( "\tif { $%s == 1 } then { set %s \"%s\" }\n",
  1014.             cfg1->optionname, cfg->optionname, cfg1->label );
  1015.         }
  1016.     }
  1017.     }
  1018.     printf( "}\n\n\n" );
  1019.  
  1020.     printf( "proc update_define { } {\n" );
  1021.     clear_globalflags( scfg );
  1022.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  1023.     {
  1024.     if ( cfg->token == token_define_bool )
  1025.     {
  1026.         cfg->global_written = 1;
  1027.         printf( "\tglobal %s\n", cfg->optionname );
  1028.     }
  1029.     }
  1030.  
  1031.     for ( cfg = scfg; cfg != NULL; cfg = cfg->next )
  1032.     {
  1033.     if( cfg->token == token_define_bool )
  1034.     {
  1035.         if ( cfg->cond == NULL )
  1036.         printf( "\tset %s %s\n", cfg->optionname, cfg->value );
  1037.         else
  1038.         generate_if( cfg, cfg->cond, -1, 0 );
  1039.     }
  1040.     }
  1041.     printf( "}\n\n\n" );
  1042.  
  1043.     /*
  1044.      * That's it.  We are done.  The output of this file will have header.tk
  1045.      * prepended and tail.tk appended to create an executable wish script.
  1046.      */
  1047. }
  1048.